<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Myfiles extends Public_Controller
{
	private	$_path = '';
	private $_folder = '';
	private $_countdownTime =0;
	public function __construct()
	{
		parent::__construct();
		$this->load->library('session');
		/*
		$userData = $this->session->all_userData();
		if (isset($userData['logged_in']) && $userData['logged_in']){
			redirect('./');
		}
		*/
		
		$this->load->model('uploking_files_m');
		$this->load->model('uploking_downloads_m');
		$this->load->model('uploking_ratings_m');
		
		$this->load->library(array('keywords/keywords'));
		$this->lang->load('uploking');
		$this->load->helper('uploking');
		$this->config->load('uploking');
		$this->_path = FCPATH . $this->config->item('files_folder') . DIRECTORY_SEPARATOR;
		$this->_folder = $this->config->item('files_folder') . URL_SEPARATOR;
		$this->_countdownTime = $this->config->item('countdown_time');
	}

	public function index($fileId = 0)
	{	
		$file = $this->uploking_files_m->get($fileId);
		if(!$file){
			redirect('./');
		}
		$userData = $this->session->all_userData();
		if (isset($userData['logged_in']) && $userData['logged_in']){
			$isLogged = true;
		}else{
			$isLogged = false;
		}
		
		$videoExtList = $this->config->item('video_extension_list');
		if(in_array($file->extension, $videoExtList)){
			$isVideo = true;
		}else{
			$isVideo = false;
		}
		$videoPath = $this->config->item('files_video_folder');
		$this->template
		->title($this->module_details['name'])
		->append_metadata( js('myfiles.js', 'myfiles') )
		->set('countdownTime', $this->_countdownTime)
		->set('isVideo', $isVideo)
		->set('videoPath', $videoPath)
		->set('file', $file)
		->set('fileId', $fileId)
		->set('isLogged', $isLogged)
		->build('download', $this->data);
	}
	public function download()
	{
		$result = array();
		$captcha = $this->input->post('captcha');
		$correctCaptcha 	= $this->session->userdata('captcha_content');
		$expTime			= $this->session->userdata('captcha_exprise_time');
		$result['result'] = true;
		if( $correctCaptcha != $captcha){
			$result['result'] = false;
			$result['message'] = lang('uploking_file_invalid_captcha_label');
		}else if( time() > $expTime ){
			$result['result'] = false;
			$result['message'] = lang('uploking_file_invalid_captcha_expired_label');
		}
		
		if($result['result']){
			$fileId = $this->input->post('file_id');
			$file = $this->uploking_files_m->get($fileId) OR show_404();
			
			$filename = $file->filename;
			$fulPath = site_url().$this->_folder. $file->folder. URL_SEPARATOR .$filename;
			
			$this->uploking_files_m->update($fileId, array(
				'download'		=> $file->download + 1
			));
			
			$ip = $this->input->ip_address();
			
			$lastDownload = $this->uploking_downloads_m->where('ip', $ip)->where('status', DOWNLOAD_STATUS_VALID)->order_by('download_time', 'DESC')->get_by('file_id',$fileId);
			if($lastDownload){
				if(time() - $lastDownload->download_time > DOWNLOAD_VALID_TIME * 60 * 60){
					$status = DOWNLOAD_STATUS_VALID;
				}else{
					$status = DOWNLOAD_STATUS_INVALID;
				}
			}else{
				$status = DOWNLOAD_STATUS_VALID;
				
			}
			
			$this->uploking_downloads_m->insert(array(
				'file_id' 		=> $fileId,
				'user_id' 		=> $file->user_id,
				'ip' 			=> $ip,
				'status'		=> $status,
				'download_time' => time(),
			));
			$result['url'] = $fulPath; 
		}
		
		echo json_encode($result);
		
	}
	public function get($fileId)
	{
		$this->load->library('session');
		$userData = $this->session->all_userData();
		if((isset($userData['logged_in']) && $userData['logged_in']) || ($userData['group'] == 'admin')){
			if(isset($userData['account'])){
				$file = $this->uploking_files_m->where('user_id', $userData['account']['user_id'])->get($fileId) OR show_404();
			}else{
				$file = $this->uploking_files_m->get($fileId) OR show_404();
			}
			$filename = $file->filename;
			$fulPath = site_url().$this->_folder. $file->folder. URL_SEPARATOR .$filename;
			header("Location: $fulPath");
		}else{
			redirect('./');
		}
	}
	
	public function get_download_form($fileId)
	{
		$file = $this->uploking_files_m->get($fileId);
		if(!$file){
			die();
		}
		$userData = $this->session->all_userData();
		if (isset($userData['logged_in']) && $userData['logged_in']){
			$isLogged = true;
		}else{
			$isLogged = false;
		}
		$this->input->is_ajax_request() ? $this->template->set_layout(FALSE) : '';
		$this->template
		->title($this->module_details['name'])
		->set('file', $file)
		->set('fileId', $fileId)
		->set('isLogged', $isLogged)
		->build('download_form', $this->data);
	}
	public function rating(){
		$fileId = $this->input->post('file_id');
		$rating = $this->input->post('rating');
		$result = array();
		if(!$rating || !$fileId){
			$result['result']	= false;
			$result['message']	= lang('uploking_rating_required_label');
		}else{
			$file = $this->uploking_files_m->get($fileId);
			if(!$file){
				die();
			}
			$ip = $this->input->ip_address();
			
			$this->uploking_ratings_m->insert(array(
	  			'file_id'	=> $fileId ,
	  			'user_id' 	=> $file->user_id ,
				'ip' 		=> $ip ,
				'rating' 	=> $rating,
				'rating_time' => time()
			));
			$result['result']	= true;
			$result['message']	= lang('uploking_rating_thankyou_label');
		}
		echo json_encode($result);
		
	}
	public function get_rating_form($fileId)
	{
		$file = $this->uploking_files_m->get($fileId);
		if(!$file){
			die();
		}
		
		$this->input->is_ajax_request() ? $this->template->set_layout(FALSE) : '';
		$this->template
		->title($this->module_details['name'])
		->set('file', $file)
		->set('fileId', $fileId)
		->build('rating_form', $this->data);
	}
	
	public function play($id = 0){
		$this->input->is_ajax_request() ? $this->template->set_layout(FALSE) : '';
		$file = $this->uploking_files_m->get($id);
		
		if($this->input->is_ajax_request()){	
			$this->uploking_files_m->update($id, array(
				'download'		=> $file->download + 1
			));
			
			$ip = $this->input->ip_address();
			
			$lastDownload = $this->uploking_downloads_m->where('ip', $ip)->where('status', DOWNLOAD_STATUS_VALID)->order_by('download_time', 'DESC')->get_by('file_id',$id);
			if($lastDownload){
				if(time() - $lastDownload->download_time > DOWNLOAD_VALID_TIME * 60 * 60){
					$status = DOWNLOAD_STATUS_VALID;
				}else{
					$status = DOWNLOAD_STATUS_INVALID;
				}
			}else{
				$status = DOWNLOAD_STATUS_VALID;
			}
			
			$this->uploking_downloads_m->insert(array(
				'file_id' 		=> $id,
				'user_id' 		=> $file->user_id,
				'ip' 			=> $ip,
				'status'		=> $status,
				'download_time' => time(),
			));
		}
		
		$videoPath = $this->config->item('files_video_folder');
		$this->template
		->title($this->module_details['name'])
		->set('file', $file)
		->set('videoPath', $videoPath)
		->build('play', $this->data);
	}
	public function refesh_captcha(){
		$result = array();
		$result['result'] = true;
		$result['data'] = getCaptcha();
		echo json_encode($result);
	}
	public function validate_captcha(){
		$result = array();
		$captcha = $this->input->post('captcha');
		$correctCaptcha 	= $this->session->userdata('captcha_content');
		$expTime			= $this->session->userdata('captcha_exprise_time');
		$result['result'] = true;
		if( $correctCaptcha != $captcha){
			$result['result'] = false;
			$result['message'] = lang('uploking_file_invalid_captcha_label');
		}else if( time() > $expTime ){
			$result['result'] = false;
			$result['message'] = lang('uploking_file_invalid_captcha_expired_label');
		}
		echo json_encode($result);
	}
}
